
#import "UIImage+FFIconFont.h"
#import "FFIconFont.h"
#import <CoreText/CoreText.h>

@implementation UIImage (FFIconFont)

//+ (UIImage *)iconWithInfo:(FFIconInfo *)info {
//    CGFloat size = info.size;
//    CGFloat scale = [UIScreen mainScreen].scale;
//    CGFloat realSize = size * scale;
//    UIFont *font = [FFIconFont fontWithSize:realSize];
//    UIGraphicsBeginImageContext(CGSizeMake(realSize, realSize));
//    CGContextRef context = UIGraphicsGetCurrentContext();
//
//    if ([info.text respondsToSelector:@selector(drawAtPoint:withAttributes:)]) {
//        /**
//         * 如果这里抛出异常，请打开断点列表，右击All Exceptions -> Edit Breakpoint -> All修改为Objective-C
//         * See: http://stackoverflow.com/questions/1163981/how-to-add-a-breakpoint-to-objc-exception-throw/14767076#14767076
//         */
//        [info.text drawAtPoint:CGPointZero withAttributes:@{NSFontAttributeName:font, NSForegroundColorAttributeName: info.color}];
//    } else {
//
//#pragma clang diagnostic push
//#pragma clang diagnostic ignored "-Wdeprecated-declarations"
//        CGContextSetFillColorWithColor(context, info.color.CGColor);
//        [info.text drawAtPoint:CGPointMake(0, 0) withFont:font];
//#pragma clang pop
//    }
//
//    UIImage *image = [UIImage imageWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:scale orientation:UIImageOrientationUp];
//    UIGraphicsEndImageContext();
//
//    return image;
//}

+ (UIImage *)iconWithIcon:(NSString *)iconString fontSize:(CGFloat)size color:(UIColor *)color
{
    if (!color) {
        color = [UIColor blackColor];
    }
    CGFloat scale = [UIScreen mainScreen].scale;
    CGFloat realSize = size * scale;
    UIFont *font = [FFIconFont fontWithSize:realSize];
    UIGraphicsBeginImageContext(CGSizeMake(realSize, realSize));
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    if ([iconString respondsToSelector:@selector(drawAtPoint:withAttributes:)]) {
        /**
         * 如果这里抛出异常，请打开断点列表，右击All Exceptions -> Edit Breakpoint -> All修改为Objective-C
         * See: http://stackoverflow.com/questions/1163981/how-to-add-a-breakpoint-to-objc-exception-throw/14767076#14767076
         */
        [iconString drawAtPoint:CGPointZero withAttributes:@{NSFontAttributeName:font, NSForegroundColorAttributeName:color}];
    } else {
        
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        CGContextSetFillColorWithColor(context, color.CGColor);
        [iconString drawAtPoint:CGPointMake(0, 0) withFont:font];
#pragma clang pop
    }
    
    UIImage *image = [UIImage imageWithCGImage:UIGraphicsGetImageFromCurrentImageContext().CGImage scale:scale orientation:UIImageOrientationUp];
    UIGraphicsEndImageContext();
    
    return image;

}

@end
